﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string printServer;

        public Form1()
        {
            InitializeComponent();
            constructPrinterList();
            targetPCName.KeyPress += new KeyPressEventHandler(listenForEnter);

        }

        private void constructPrinterList()
        {
            Form2 printServerDialog = new Form2(this);
            printServerDialog.ShowDialog();
            while (printServerDialog.Visible)
                this.Enabled = false;

            MessageBox.Show("Please select the printers you wish to install or uninstall.", "Notice");
            using (Process queryPrintServer = new Process())
            {
                ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\System32\net.exe");
                info.UseShellExecute = false;
                info.CreateNoWindow = true;
                info.RedirectStandardOutput = true;
                info.RedirectStandardInput = true;
                //MessageBox.Show(printServer);
                info.Arguments = "view \\\\" + printServer + "\\";

                List<string> output = new List<string>();
                queryPrintServer.StartInfo = info;


                queryPrintServer.Start();
                queryPrintServer.WaitForExit();

                //get a list of printer shares from the print server and cut out only the printer's name
                if (queryPrintServer.HasExited)
                {
                    using (StreamReader myOutput2 = queryPrintServer.StandardOutput)
                    {

                        while (!myOutput2.EndOfStream)
                        {
                            string line = myOutput2.ReadLine();
                            int index = line.IndexOf(" ");
                            if (index > 0)
                                line = line.Substring(0, index);
                            if (line.Length > 7 && !line.Contains("---"))
                                output.Add(line);
                            Console.WriteLine(line);
                        }

                    }
                }
                foreach (string item in output)
                {
                    printerListBox.Items.Add(item);
                }
            }
        }
        public void setPrintServer(string serverName)
        {
            this.printServer = serverName;
        }

        private void listenForEnter(object sender, KeyPressEventArgs e)
        {
            if (targetPCName.Text != null)
            {
                if (e.KeyChar == (char)Keys.Enter)
                {
                    if (deleteCheckbox.Checked)
                    {
                        installPrinters("d");
                    }
                    else
                    {
                        installPrinters("a");
                    }
                }
            }
        }

        //runs the command to install printers to a machine (all new users will inherit those printers)
        private void installPrinters(string addOrDelete)
        {
            using (Process _installPrinters = new Process())
            {
                ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\System32\rundll32.exe");
                info.RedirectStandardInput = true;
                info.RedirectStandardOutput = true;
                info.UseShellExecute = false;
                info.CreateNoWindow = false;
                info.RedirectStandardError = true;

                _installPrinters.StartInfo = info;
                //_installPrinters.ErrorDataReceived += new DataReceivedEventHandler(_installPrinters_ErrorDataReceived);


                foreach (string printer in printerListBox.CheckedItems)
                {

                    info.Arguments = "Printui.dll,PrintUIEntry /g" + addOrDelete + " /c\\\\" + targetPCName.Text + " /n\\\\" + printServer + "\\" + printer;
                    //MessageBox.Show(info.Arguments);
                    _installPrinters.Start();
                    //MessageBox.Show("PC: " + targetPCName.Text + " Printer: " + printer + "\n Installing Printer Now.");
                    _installPrinters.WaitForExit();
                    _installPrinters.ErrorDataReceived += new DataReceivedEventHandler(_installPrinters_ErrorDataReceived);

                    restartSpooler();
                }
            }
            

        }

        private void restartSpooler()
        {
            using (Process _restartSpooler = new Process())
            {
                ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\System32\net.exe");
                info.RedirectStandardInput = true;
                info.RedirectStandardOutput = true;
                info.UseShellExecute = false;
                info.CreateNoWindow = false;
                info.RedirectStandardError = true;

                info.Arguments = "start /wait sc \\\\" + targetPCName + " stop spooler";

                _restartSpooler.StartInfo = info;
                _restartSpooler.Start();
                _restartSpooler.WaitForExit();

                info.Arguments = "start /wait sc \\\\" + targetPCName + " start spooler";
                _restartSpooler.StartInfo = info;
                _restartSpooler.Start();
                _restartSpooler.WaitForExit();
            }

        }


        void _installPrinters_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            MessageBox.Show("Something has gone wrong\n"+e.Data,"error");
        }

        private void goButton_Click(object sender, EventArgs e)
        {
            if (targetPCName.Text != null)
            {
                    if (deleteCheckbox.Checked)
                    {
                        installPrinters("d");
                    }
                    else
                    {
                        installPrinters("a");
                    }
            }
        }


    }
}


